<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8">
		<meta http-equiv="X-UA-Compatible" content="IE=edge">
		<meta name="viewport" content="width=device-width, initial-scale=1.0">
		<link rel="stylesheet" href="./framework/font-awesome-4.7.0/css/font-awesome.min.css">
		<link rel="stylesheet" href="./framework/reset.css">
		<link rel="stylesheet" href="./framework/common.css">
		<script src="js/base.js"></script>
		<title>东软环保公众监督系统-公众监督员端-注册</title>
	</head>
	<body>
		<!-- 总容器 -->
		<div class="wrapper">
			<div class="header-box">
				<header>
					<i class="fa fa-angle-left" onclick="history.go(-1)"></i>
					<p>注册</p>
					<i></i>
				</header>
				<div class="top-ban"></div>
				<img src="./img/header.png">
			</div>

			<table class="table-form">
				<tr>
					<td>手机号码</td>
					<td><input id="telId" type="text" placeholder="请输入手机号码"></td>
				</tr>
				<tr>
					<td>真实姓名</td>
					<td><input id="realName" type="text" placeholder="真实姓名便于我们联系您"></td>
				</tr>
				<tr>
					<td>出生日期</td>
					<td><input id="birthday" type="date" placeholder="出生日期"></td>
				</tr>
				<tr>
					<td>性别</td>
					<td>
						<input name="sex" value="1" type="radio" checked>男
						<input name="sex" value="0" type="radio">女
					</td>
				</tr>
				<tr>
					<td>密码</td>
					<td><input id="password" type="password" placeholder="请输入密码"></td>
				</tr>
				<tr>
					<td>确认密码</td>
					<td><input id="repassword" type="password" placeholder="请再次输入密码"></td>
				</tr>
				<tr>
					<td colspan="2">
						<div class="btn" onclick="SignUp()">注册</div>
					</td>
				</tr>
			</table>
		</div>
	</body>
</html>
<script>
	function SignUp() {
		let url = "http://localhost:8080/supervisor/SignUp"
		let json = {};
		let telId = document.getElementById("telId").value;
		let realName = document.getElementById("realName").value;
		let birthday = document.getElementById("birthday").value;
		let password = document.getElementById("password").value;
		let repassword = document.getElementById("repassword").value;
		if (!isValidPhoneNumber(telId)) {
			alert("请输入有效的手机号");
			return; // 如果手机号无效，则退出函数  
		}
		// 验证密码和确认密码是否一致  
		if (password !== repassword) {
			alert("密码和确认密码不一致，请重新输入");
			return; // 如果密码不一致，则退出函数  
		}
		json.telId = telId;
		json.realName = realName;
		json.birthday = birthday;
		json.password = password;
		json.repassword = repassword;
		let radios = document.getElementsByName("sex");
		let sex = "";
		for (let i = 0; i < radios.length; i++) {
			if (radios[i].checked) {
				sex = radios[i].value;
				break; // 找到后退出循环
			}
		}
		json.sex = sex;
		rest(url, "post", json, function(r) {
			if (r.ok) {
				alert("注册成功");
				// location.href = "SignIn.html"
			} else if (r.code == 501) {
				alert(r.msg);
			} else {
				alert("注册失败");
			}
		})
	}

	function isValidPhoneNumber(phoneNumber) {
		const regex = /^1[3-9]\d{9}$/;
		return regex.test(phoneNumber);
	}
</script>